using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace FederationMembershipDB
{
	public class Cheque
	{
		string _chequeNumber;
		double _amount;
		string _drawer;
		string _bank;

        public string ChequeNumber
        { get { return _chequeNumber; } set { _chequeNumber = value; } }
        public double Amount
        { get { return _amount; } set { _amount = value; } }
        public string Drawer
        { get { return _drawer; } set { _drawer = value; } }
        public string Bank
        { get { return _bank; } set { _bank = value; } }

		public void AddCheque(ref int chequeID)
		{
			//Parameter: ref int ChequeID - when cheque ID is set, the int variable passed in will be updated in the calling function
            
            //           SqlTransaction - to keep the transaction as a whole unit, pass in an existing Transaction
            
            //Get the last ID in the DB
            string sqlSelectID = "SELECT TOP(1) ID FROM Cheque Order By ID DESC;";
            DataSet ds = Utilities.SearchDatabase(sqlSelectID);

            //check to see if it will be the first item inserted.
            if (ds.Tables[0].Rows.Count == 0)
            {
                chequeID = 0;
            }
            else
            {
                chequeID = int.Parse(ds.Tables[0].Rows[0].ItemArray.GetValue(0).ToString()) + 1; // +1 to increment - the next chequeID
            }

            //Construct INSERT statement
            string sqlInsert = "INSERT INTO Cheque VALUES ('" + chequeID + "','" + ChequeNumber + "','" +  Amount + "','" + Drawer + "','" + Bank + "');";

            Utilities.DatabaseNonQuery(sqlInsert);

		}
	}
}
